//
//  MV-tableViewCell-one.m
//  22-homework-QQMV-ALi
//
//  Created by 王道林 on 16/8/13.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "MV-tableViewCell-one.h"
#import "BaseCollectionView.h"
#import "MVCollectionViewCell.h"
#import "List-Model.h"
#import "MVCollectionReusableView.h"
@interface MV_tableViewCell_one ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) BaseCollectionView *CollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;


@end

@implementation MV_tableViewCell_one

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

- (void)createSubView
{
    self.contentView.backgroundColor = [UIColor whiteColor];
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumLineSpacing = 5;
    _layout.minimumInteritemSpacing = 10;
    _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    _CollectionView = [[BaseCollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:_layout];
    _CollectionView.backgroundColor = [UIColor whiteColor];
    _CollectionView.delegate = self;
    _CollectionView.dataSource = self;
    [self.contentView addSubview:_CollectionView];
    // 注册cell
    [_CollectionView registerNib:[UINib nibWithNibName:@"MVCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"MVCollectionPool"];
    // 注册header
    [_CollectionView registerNib:[UINib nibWithNibName:@"MVCollectionReusableView"bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake(self.contentView.bounds.size.width - 20, self.contentView.frame.size.height / 3);
    _CollectionView.frame = self.contentView.bounds;
    
}


// 协议方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _listModel.mvlist.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   MVCollectionViewCell *cell = [_CollectionView dequeueReusableCellWithReuseIdentifier:@"MVCollectionPool" forIndexPath:indexPath];
   MVListModel *mvList = _listModel.mvlist[indexPath.row];
    cell.MVlist = mvList;
    return cell;
}



// 每个item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return CGSizeMake(self.contentView.bounds.size.width - 20, self.contentView.bounds.size.height * 5 / 12 - 20);
    }
    else
    {
        return CGSizeMake((self.contentView.bounds.size.width - 30) / 2, self.contentView.frame.size.height / 4 - 10);
    }
}


// set方法
- (void)setListModel:(List_Model *)listModel
{
    _listModel = listModel;
    [_CollectionView reloadData];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    MVCollectionReusableView *View = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    View.lable.text = _listModel.name;
    return View;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.contentView.frame.size.width, self.contentView.frame.size.height / 12 );
}


@end
